package com.nf.practice.exportExcel;

import com.alibaba.excel.EasyExcel;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.junit.jupiter.api.Test;
import org.springframework.util.CollectionUtils;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;

public class SfDataExportTest2 {

//    private final String dmsUrl = "http://172.16.10.5:31314/service";
private final String dmsUrl = "http://192.168.40.49:1688/service";


    @Test
    public void batchExport(){
        String fileName = "E:/SFDSJ/cfsdb(1).xls";
        // 读取到的数据
        List<SfFile2> list = EasyExcel.read(fileName)
                .head(SfFile2.class)
                .sheet()
                .doReadSync();
        Long begin = System.currentTimeMillis();
        for (int i = 0; i < list.size(); i++) {
            String appNo = list.get(i).getAppNo();
            String name = list.get(i).getName();
            String idNo = list.get(i).getIdCard();
            String jsonData = list.get(i).getJsonData();
            String model = list.get(i).getModel();


            System.out.println(appNo);
            System.out.println(idNo);
            System.out.println(name);
            List<SxDataDetail> result = dataExport(appNo, idNo, name);
            System.out.println("=========个人失信Excel导出开始========");
            String filePath = "E:/SFDSJ/" + name + idNo.replaceAll("(?<=\\w{6})\\w(?=\\w{4})", "*") + "失信.xlsx";
            EasyExcel.write(filePath, SxDataDetail.class).sheet(1).doWrite(result);
            System.out.println("=========个人失信Excel导出结束========");
            System.out.println("计算耗时：" + (System.currentTimeMillis() - begin) + "ms");
        }

    }

//    @Test
    public List<SxDataDetail> dataExport(String appNo,String idNo,String name) {
//

        // 2、调用dms
        // 2.1、失信模型
//        JSONObject sxJsonData = getSfData(reqMap,"SXBZXR");
//        JSONObject xgJsonData = getSfData(reqMap,"XGBZXR");


        // SS01本人失信模型
        List<SxDataDetail> sxDataList = new ArrayList<>();
        JSONObject ss01SxData = null;
//        if (null != sxJsonData && !sxJsonData.isEmpty()) {
//            ss01SxData = sxJsonData.getJSONObject("data");
//        }
        if (null != ss01SxData && !ss01SxData.isEmpty()) {
            JSONObject ss01SxRes = ss01SxData.getJSONObject("data");
            if (null != ss01SxRes && !ss01SxRes.isEmpty()) {
                JSONArray sxbzxrArr = ss01SxRes.getJSONArray("sxbzxr");
                if (!CollectionUtils.isEmpty(sxbzxrArr)) {
                    for (int i = 0; i < sxbzxrArr.size(); i++) {
                        JSONObject json = sxbzxrArr.getJSONObject(i);
                        if (null != json && !json.isEmpty()) {
                            SxDataDetail sxDatadetail = new SxDataDetail();
                            String id = json.getString("id");
                            String xb = json.getString("xb");
                            String zzjgdm = json.getString("zzjgdm");
                            String qyfr = json.getString("qyfr");
                            String zxfy = json.getString("zxfy");
                            String sf = json.getString("sf");
                            String zxyjwh = json.getString("zxyjwh");
                            String larq = json.getString("larq");
                            String ah = json.getString("ah");
                            String zxyjdw = json.getString("zxyjdw");
                            String yw = json.getString("yw");
                            String lxqk = json.getString("lxqk");
                            String xwqx = json.getString("xwqx");
                            String fbrq = json.getString("fbrq");
                            String pjje_gj = json.getString("pjje_gj");
                            sxDatadetail.setType("失信");
                            sxDatadetail.setName(name);
                            sxDatadetail.setIdNo(idNo.replaceAll("(?<=\\w{6})\\w(?=\\w{4})", "*"));
                            sxDatadetail.setId(id);
                            sxDatadetail.setXb(xb);
                            sxDatadetail.setZzjgdm(zzjgdm);
                            sxDatadetail.setQyfr(qyfr);
                            sxDatadetail.setZxfy(zxfy);
                            sxDatadetail.setSf(sf);
                            sxDatadetail.setZxyjwh(zxyjwh);
                            sxDatadetail.setLarq(larq);
                            sxDatadetail.setAh(ah);
                            sxDatadetail.setZxyjdw(zxyjdw);
                            sxDatadetail.setYw(yw);
                            sxDatadetail.setLxqk(lxqk);
                            sxDatadetail.setXwqx(xwqx);
                            sxDatadetail.setFbrq(fbrq);
                            sxDatadetail.setPjje_gj(pjje_gj);
                            sxDataList.add(sxDatadetail);
                        }
                    }
                }
            }
        }



        // 本人限高模型
        List<XgDataDetail> xgDataList = new ArrayList<>();
        JSONObject ss01XgData = null;
//        if (null != xgJsonData && !xgJsonData.isEmpty()) {
//            ss01XgData = xgJsonData.getJSONObject("data");
//        }
        if (null != ss01XgData && !ss01XgData.isEmpty()) {
            JSONObject ss01XgRes = ss01XgData.getJSONObject("data");
            if (null != ss01XgRes && !ss01XgRes.isEmpty()) {
                JSONArray xgbzxrArr = ss01XgRes.getJSONArray("xgbzxr");
                if (!CollectionUtils.isEmpty(xgbzxrArr)) {
                    for (int i = 0; i < xgbzxrArr.size(); i++) {
                        SxDataDetail xgDataDetail = new SxDataDetail();
                        JSONObject json = xgbzxrArr.getJSONObject(i);
                        if (null != json && !json.isEmpty()) {
                            String qymc = json.getString("qymc");
                            String qyfr = json.getString("qyfr");
                            String id = json.getString("id");
                            String zxfy = json.getString("zxfy");
                            String ah = json.getString("ah");
                            String larq = json.getString("larq");
                            String fbrq = json.getString("fbrq");

                            xgDataDetail.setType("限高");
                            xgDataDetail.setQymc(qymc);
                            xgDataDetail.setName(name);
                            xgDataDetail.setIdNo(idNo.replaceAll("(?<=\\w{6})\\w(?=\\w{4})", "*"));
                            xgDataDetail.setId(id);
                            xgDataDetail.setQyfr(qyfr);
                            xgDataDetail.setZxfy(zxfy);
                            xgDataDetail.setLarq(larq);
                            xgDataDetail.setAh(ah);
                            xgDataDetail.setFbrq(fbrq);
                            sxDataList.add(xgDataDetail);
                        }
                    }
                }
            }
        }

        return sxDataList;
    }

    private List<Map<String, Object>> getSxbzxrData(JSONArray originArr) {
        List<Map<String, Object>> resData = new ArrayList<>();
        for (int i = 0; i < originArr.size(); i++) {
            Map<String, Object> map = new HashMap<>();
            JSONObject json = originArr.getJSONObject(i);
            if (null != json && !json.isEmpty()) {
                map.put("id",json.getString("id"));
                map.put("xb",json.getString("xb"));
                map.put("zzjgdm",json.getString("zzjgdm"));
                map.put("qyfr",json.getString("qyfr"));
                map.put("zxfy",json.getString("zxfy"));
                map.put("sf",json.getString("sf"));
                map.put("zxyjwh",json.getString("zxyjwh"));
                map.put("larq",json.getString("larq"));
                map.put("ah",json.getString("ah"));
                map.put("zxyjdw",json.getString("zxyjdw"));
                map.put("yw",json.getString("yw"));
                map.put("lxqk",json.getString("lxqk"));
                map.put("xwqx",json.getString("xwqx"));
                map.put("fbrq",json.getString("fbrq"));
                map.put("pjje_gj",json.getString("pjje_gj"));
                resData.add(map);
            }
        }
        return resData;
    }


    private JSONObject getSfData(Map<String,Object> reqMap,String model) {
        reqMap.put("model", model);
        String json = JSON.toJSONString(reqMap);
        CloseableHttpClient client = HttpClients.createDefault();
        HttpPost post = new HttpPost(dmsUrl + "/SS01");
        String resStr = null;
        try {
            StringEntity s = new StringEntity(json, "UTF-8");
            s.setContentEncoding("UTF-8");
            s.setContentType("application/json");//发送json数据需要设置contentType
            post.setEntity(s);
            HttpResponse res = client.execute(post);
            if(res.getStatusLine().getStatusCode() == HttpStatus.SC_OK){
                resStr = EntityUtils.toString(res.getEntity(), "UTF-8");
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }

        JSONObject jsonData = new JSONObject();
        if (StringUtils.isNotBlank(resStr)) {
            jsonData = JSONObject.parseObject(resStr);
        }
        return jsonData;
    }

}
